# -*- coding: utf-8 -*-
# @Time    : 2018-05-21 19:28
# @Author  : Shi YuCen
# @Email   : shiyucen@bbdservice.com
# @File    : TinyCompanyLoanCnt.py
# @Software: PyCharm

'''eof
name:小额贷款公司未结清贷款笔数
code:TinyCompanyLoanCnt
tableName:
columnName:
groups:个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', None, 'null', '/', 'Null']
credit_list=[u'正常',u'逾期',u'呆账']

def FormatData(table_name):
    try:
        if IC in null_type_list:
            return [{}]
        if len(IC) == 0:
            return [{}]
        icr = json.loads(IC[0]["icr"])[table_name]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]
def  TinyCompanyLoanCnt():
    try:
        Sum_list = FormatData("ICRLoanInfo002*Loan")
        if Sum_list == [{}]:
            return u'缺失值'
        return_list = []
        for Sum in Sum_list:
            if  Sum['State'] in null_type_list and Sum['State'] in null_type_list:
                return_list.append(None)
            elif  Sum['State'] in credit_list and Sum['FinanceType'] == u'小额信贷公司':
                return_list.append(1)
            else:
                return_list.append(0)
        return_list_not_none = filter(lambda x:x is not None, return_list)
        if len(return_list_not_none) == 0:
            return u'缺失值'
        else:
            return sum(return_list_not_none)
    except:
        return u'缺失值'

result = TinyCompanyLoanCnt()


